package com.googlecode.framework.core.bean;


/**
 * Label-Value格式的JavaBean
 * 
 * @author xiayg
 *
 * @param <T>
 */
public class LabelValueBean<T> implements Comparable<LabelValueBean<T>>, java.io.Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 7697289611604823406L;
	
    private String label;

    private T value;

    public LabelValueBean() {
        super();
    }

    public LabelValueBean(String label, T value) {
        this.label = label;
        this.value = value;
    }
    
	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public T getValue() {
		return value;
	}

	public void setValue(T value) {
		this.value = value;
	}

	@Override
	public int compareTo(LabelValueBean<T> o) {
        String otherLabel = ((LabelValueBean<T>) o).getLabel();
        return this.getLabel().compareTo(otherLabel);
	}
	
	@Override
    public String toString() {
        StringBuffer sb = new StringBuffer("LabelValueBean[");

        sb.append(this.label);
        sb.append(", ");
        sb.append(this.value);
        sb.append("]");

        return (sb.toString());
    }
	
	@Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof LabelValueBean<?>)) {
            return false;
        }

        LabelValueBean<?> bean = (LabelValueBean<?>) obj;
        int nil = (this.getValue() == null) ? 1 : 0;

        nil += ((bean.getValue() == null) ? 1 : 0);

        if (nil == 2) {
            return true;
        } else if (nil == 1) {
            return false;
        } else {
            return this.getValue().equals(bean.getValue());
        }
    }

    @Override
    public int hashCode() {
        return (this.getValue() == null) ? 17 : this.getValue().hashCode();
    }

}
